package com.wuxiaoqin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuxiaoqin.admin.entity.Ad;

import com.wuxiaoqin.admin.mapper.AdMapper;
import com.wuxiaoqin.admin.service.IAdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wuxiaoqin.uitls.respObj.RespBean;
import com.wuxiaoqin.uitls.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-10-29
 */
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements IAdService {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired(required = false)
    private AdMapper adMapper;



    @Override
    public RespBean login(String username, String password, HttpServletRequest request) {
//    public RespBean login(String username, String password, String code, HttpServletRequest request) {

//        String captcha = (String) request.getSession().getAttribute("captcha");
//        System.out.println(captcha);
//        if (code.isEmpty()||!captcha.equalsIgnoreCase(code)){
//            return RespBean.error("验证码输入错误，请重新输入");
//        }
        UserDetails userDetails = null;
        try{
            userDetails = userDetailsService.loadUserByUsername(username);
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("用户名不存在");
        }
        Ad ad = getUsersByAdminId(username);
        System.out.println(userDetails.getPassword());
        if (userDetails == null || !passwordEncoder.matches(password,userDetails.getPassword())){
            return RespBean.error("密码不正确");
        }
        //更新security登录用户对象
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //System.out.println(authenticationToken);
        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);

        return RespBean.success("登录成功",tokenMap);
    }
    /**
     * 按id 查询
     * @param aId
     * @return
     */
    @Override
    public Ad getUsersByAdminId(String aId) {
        return adMapper.selectOne(new QueryWrapper<Ad>().eq("A_id", aId));
    }
}
